package com.free.fs.controller;

import com.free.fs.mapper.FileMapper;
import com.free.fs.model.FilePojo;
import com.free.fs.model.User;
import com.free.fs.service.impl.ScoreServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.List;

public class BaseController {

    @Autowired
    FileMapper fileMapper;

    @Autowired
    ScoreServiceImpl scoreService;

    /**
     * 获取当前登录的user
     */
    public User getLoginUser() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            Object object = subject.getPrincipal();
            if (object != null) {
                return (User) object;
            }
        }
        return null;
    }

    /**
     * 获取当前登录的userId
     */
    public Long getLoginUserId() {
        User loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getId();
    }

    /**
     * 获取分类的文件集合
     */
    public  void getListByType(Model model){
        List<FilePojo> list1= new ArrayList<>();
        List<FilePojo>  list2= new ArrayList<>();
        List<FilePojo>  list3= new ArrayList<>();
        int score = 0;
        List<FilePojo> filePojos = fileMapper.selectAll();
        for (FilePojo filePojo : filePojos) {
            Boolean isScored = filePojo.getIsScored();
            Boolean isDir = filePojo.getIsDir();
            if(isScored){
                score = scoreService.queryScoreByFId(filePojo.getId()).getScore();
            }
            if (!isDir && !isScored) {//未审核文件
                list1.add(filePojo);
            } else if(!isDir && score > 60){//合格文件
                list2.add(filePojo);
            } else if (!isDir && score < 60){//不合格文件
                list3.add(filePojo);
            }
        }
        model.addAttribute("list1",list1);
        model.addAttribute("list2",list2);
        model.addAttribute("list3",list3);
        System.out.println("未审核文件===>"+list1);
        System.out.println("合格文件===>"+list2);
        System.out.println("不合格文件===>"+list3);
    }
}
